/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bitaka.ws;

import dao.BilletDAO;
import dao.EvenementDAO;
import dao.KitDAO;
import dao.OnligneDAO;
import dao.TransDAO;
import dao.TypeBilletDAO;
import dao.TypebDAO;
import entity.Billet;
import entity.Evenement;
import entity.Kit;
import entity.Onligne;
import entity.Trans;
import entity.Trans_1;
import entity.Typeb;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "VerifyPass")
public class VerifyPass {

    @EJB
    BilletDAO billetDAO = new BilletDAO();
    List<Billet> lists;
    Evenement evenement;
    Date date = new Date();
    @EJB
    EvenementDAO evenementDAO = new EvenementDAO();
    TypebDAO typebDAO = new TypebDAO();
    OnligneDAO onDAO = new OnligneDAO();
    // TransDAO transDAO = new TransDAO();
    DBTransaction dbtrans = new DBTransaction();
    File[] fichiers = null;
    File directoryToScan = new File("C:\\OnLigne");

    @WebMethod(operationName = "verifyTicket")
    public String verifyTicket(@WebParam(name = "key") String key, @WebParam(name = "type") String type) throws ParseException {
        System.out.println("------------------------------------------------------------------------");
        key = key.replaceAll("=", "-");
        date = new Date();
        Billet b;
        Onligne o = new Onligne();
        date.setHours(date.getHours() - 7);

        System.out.println("Date=" + date);
        evenement = (Evenement) evenementDAO.findByProperty("date", date).get(0);
        System.out.println("Evenement e" + evenement.getTitre());
        System.out.println("Date=" + date);
        Trans trans = new Trans();
        trans.setDate(new Date());
        trans.setTime(new Date());
        trans.setPorte(type);
        trans.setNumero(key);

        lists = billetDAO.findByProperty("numero", key);

        //ONLINE TEST--------------------------------------------------------------
        if (key.equals("068072370304060412") || key.equals("40470205268520-KT88")
                || key.equals("80400205919420-KT88") //  || key.equals("50470371418310-KX22")
                ) {
            System.out.println("marwen");
            return ("OK;PassM");
        }
        if (key.contains("-XB")) {
            trans.setEtat("OK;KIT B");
            System.out.println("KIT B " + key);
            dbtrans.insertTrans(trans);
            return "OK;KIT B";
        }
        if (key.contains("-OL8")) {
            if (!key.contains("-OL84")) {
                key += "4";
                key=key.trim();
            }
            List<Onligne> s = onDAO.findByNemuro(key);
            System.out.println("Online " + s.size());
            if (s.size() > 0) {
                o = s.get(0);
                Typeb t = (Typeb) typebDAO.findByProperty("typeweb", o.getTypeweb()).get(0);
                Evenement eve = t.getEvenement();
                System.out.println("Online Evenement :" + eve.getTitre());
                if (eve.getDate().getDate() == date.getDate()) {
                    if (o.isUtilise()) {
                        trans.setEtat("NOTOK;utiliser Online");
                        System.out.println("Billet online Utilisée " + key);
                        dbtrans.insertTrans(trans);
                        return "NOTOK;utiliser Online";
                    } else {
                        o.setUtilise(true);

                        onDAO.beginTransaction();
                        onDAO.merge(o);
                        onDAO.commit();

                        System.out.println("Entrer succes payant:" + key);
                        trans.setEtat("OK;Online");
                        dbtrans.insertTrans(trans);
                        return "OK;Online";
                    }

                } else {
                    System.out.println("Spectacle invalide " + eve.getTitre());
                    trans.setEtat("NOTOK;invalide Spectacle");
                    dbtrans.insertTrans(trans);
                    return "NOTOK;invalide Spectacle";
                }


            } else {
                System.out.println("Billet online inexistante");
                trans.setEtat("NOTOK;invalide Spectacle");
                dbtrans.insertTrans(trans);
                return "NOTOK;online inexistante";
            }

        } //------------------------------------------------------------------------------
        else if (key.contains("-PP99")) {
            System.out.println("PASS PP99");
            trans.setEtat("OK;Pass");
            dbtrans.insertTrans(trans);
            return "OK;Pass";
        } //            if (key.contains("30390270721200-BA14") || key.contains("10310270023200-BA14")
        //                    ||key.contains("60370270610200-BA14")
        //                    ||key.contains("903103983790-BA14")
        //                    ) {
        //            System.out.println("PASSS PP99");
        ////            trans.setEtat("OK;Pass");
        ////            dbtrans.insertTrans(trans);
        //            return "OK;Pass";
        //        }
        else if (lists.size() < 1) {
            if (key.contains("SO19")) {
                System.out.println("OK;demi Tarif:" + key);
                trans.setEtat("OK;Demi tarif");
                dbtrans.insertTrans(trans);
                return "OK;Demi tarif";

            }
            System.out.println("NOTOK;billet inexistante:" + key);
            trans.setEtat("NOTOK;billet inexistante");
            dbtrans.insertTrans(trans);
            return "NOTOK;billet inexistante";
        } else {
            b = lists.get(0);
            System.out.println("Numero:" + b.getNumero() + "  ; type :" + b.getTypeb().getTypeBillet().getTypeCarte() + " ;id:" + b.getId()
                    + "vendu :" + b.getVendu() + " utilise :" + b.getUtilise());

            String t = b.getTypeb().getTypeBillet().getTypeCarte();
            String modele = b.getTypeb().getTypeBillet().getModele();
            

            if (key.contains("-KX22")) {
                if(b.getBnrAcces()<2)
                { int k=b.getBnrAcces();
                b.setBnrAcces(k+1);
                dbtrans.updateBillet(b);
                System.out.println("KIT GRATUIT 2 personnes reussie");
                trans.setEtat("OK;KIT GRATUIT 2 personnes reussie");
                dbtrans.insertTrans(trans);
                return "OK;KIT GRATUIT 2 personnes reussie";}
                else
                { 
                System.out.println("NOTOK;KIT GRATUIT 2 personnes utiliser");
                trans.setEtat("NOTOK;KIT GRATUIT 2 personnes utiliser");
                dbtrans.insertTrans(trans);
                return "NOTOK;KIT GRATUIT 2 personnes utiliser";
                }
                
                
            } else if (key.contains("-KX11")) {
                if(b.getBnrAcces()<1)
                { int k=b.getBnrAcces();
                b.setBnrAcces(k+1);
                dbtrans.updateBillet(b);
                System.out.println("KIT GRATUIT 1 personnes reussie");
                trans.setEtat("OK;KIT GRATUIT  1 personnes reussie");
                dbtrans.insertTrans(trans);
                return "OK;KIT GRATUIT  1 personnes reussie";}
                else
                { 
                System.out.println("NOTOK;KIT GRATUIT1 personnes utiliser");
                trans.setEtat("NOTOK;KIT GRATUIT 1 personnes utiliser");
                dbtrans.insertTrans(trans);
                return "NOTOK;KIT GRATUIT 1 personnes utiliser";
                }
            }
            if(t.equals("handicape"))
            {   if (b.getTypeb().getEvenement().equals(evenement)){
            if(b.getUtilise()==false)
                {  
                b.setUtilise(true);
                dbtrans.updateBillet(b);
                System.out.println("demi tarif Success");
                trans.setEtat("OK;demi tarif Success");
                dbtrans.insertTrans(trans);
                return "OK;demi tarif Success";}
                else
                { 
                System.out.println("NOTOK;demi tarif utiliser");
                trans.setEtat("NOTOK;demi tarif  utiliser");
                dbtrans.insertTrans(trans);
                return "NOTOK;demi tarif  utiliser";
                }
            }
            else
            {
                System.out.println("NOTOK;handicap spectacle invalide");
                trans.setEtat("NOTOK;handicap spectacle invalide");
                dbtrans.insertTrans(trans);
                return "NOTOK;handicap spectacle invalide";
            }
                
            
            }

            if (t.equals("billets payants") ) {
                System.out.println("x");
                if (!b.getVendu()) {
                    System.out.println("OK;billet non vendue:" + key);
                    trans.setEtat("OK;billet non vendue");
                    dbtrans.insertTrans(trans);
                    return "OK;billet non vendue";
                }
                if (b.getTypeb().getEvenement().equals(evenement)){
                if (b.getUtilise() == false) {
                    if (type.equals("Public")) {

                        b.setUtilise(true);
                        dbtrans.updateBillet(b);
//                        billetDAO.beginTransaction();
//                        billetDAO.merge(b);
//                        billetDAO.commit();
                        System.out.println("OK;billet payant success:" + key);
                        trans.setEtat("OK;" + modele);
                        dbtrans.insertTrans(trans);
                        return "OK;billet payant success " + modele;
                    } else if (type.equals("Vip") && modele.contains("chaise")) {
                        b.setUtilise(true);
                        dbtrans.updateBillet(b);
//                        billetDAO.beginTransaction();
//                        billetDAO.merge(b);
//                        billetDAO.commit();
                        System.out.println("OK;billet payant success chaise public a travers VIP port" + key);
                        trans.setEtat("OK;billet payant success chaise public a travers VIP port" + modele);
                        dbtrans.insertTrans(trans);
                        return "OK;billet payant success chaise public a travers VIP port " + modele;

                    } else {
                        System.out.println("NOTOK;Port Incorrecte:" + key);
                        trans.setEtat("NOTOK;Port Incorrecte ");
                        dbtrans.insertTrans(trans);
                        return "NOTOK;Port Incorrecte";
                    }
                } else {
                    System.out.println("NOTOK;utiliser:" + key);
                    trans.setEtat("NOTOK;utiliser");
                    dbtrans.insertTrans(trans);
                    return "NOTOK;utiliser";
                }
                }
                else
                
                {
                    System.out.println("NOTOK;Spectacle invalide:" + key);
                    trans.setEtat("NOTOK;Spectacle invalide");
                    dbtrans.insertTrans(trans);
                    return "NOTOK;Spectacle invalide";
                }
                


            } //-----------------------------------------invitation
            else if (t.equals("invitation") && b.getTypeb().getEvenement().equals(evenement)) {

                //if (b.getBnrAcces()==0  ) {
                System.out.println("Entrer Invitation");
                if (type.equals("Vip")||type.equals("Public")) {
                    if (b.getBnrAcces() == 0) {
//                        billetDAO.beginTransaction();
                        b.setBnrAcces(1);
                        // b.setUtilise(true);
//                        billetDAO.merge(b);
//                        billetDAO.commit();
                        dbtrans.updateBillet(b);
                        System.out.println("OK;success  invitation :" + key);
                        trans.setEtat("OK;success  invitation ");
                        dbtrans.insertTrans(trans);
                        return "OK;success  invitation " + modele;
                    } else if (b.getBnrAcces() == 1) {
                        if (b.getTypeb().getTypeBillet().getModele().contains("1")) {
                            System.out.println("NOTOK;utiliser 1 personne invitation:" + key);
                            trans.setEtat("NOTOK;utiliser 1 personne invitation");
                            dbtrans.insertTrans(trans);
                            return "NOTOK;utiliser 1 personne invitation";
                        } else if (b.getTypeb().getTypeBillet().getModele().contains("2")) {
//                            billetDAO.beginTransaction();
                            b.setBnrAcces(2);
                            dbtrans.updateBillet(b);
//                            billetDAO.merge(b);
//                            billetDAO.commit();
                            System.out.println("OK;success  invitation 2 personne :" + key);
                            trans.setEtat("OK;success  invitation 2 personne ");
                            dbtrans.insertTrans(trans);
                            return "OK;success  invitation 2 personne " + modele;
                        } else {
                            System.out.println("sans else ........//,,,llll");
                            return "sans else ........//,,,llll";
                        }


                    } else {
                        System.out.println("NOTOK;utiliser  invitation 2 personne :" + key);
                        trans.setEtat("NOTOK;utiliser  invitation 2 personne ");
                        dbtrans.insertTrans(trans);

                        return "NOTOK;utiliser  invitation 2 personne";
                    }
                } 
                //
                else {
                    System.out.println("NOTOK;Invitation port invalide :" + key);
                    trans.setEtat("NOTOK;Invitation port invalide  ");
                    dbtrans.insertTrans(trans);
                    return "NOTOK;Invitation port invalide ";
                }

            } //-------------------------------VIP
            else if (t.equals("VIP")) {
                System.out.println("Entrer VIP");
                if (type.equals("Vip")) {
                    if (b.getBnrAcces() < 2) {
//                        billetDAO.beginTransaction();
                        b.setBnrAcces(b.getBnrAcces() + 1);
                        dbtrans.updateBillet(b);
//                        billetDAO.merge(b);
//                        billetDAO.commit();

                        System.out.println("OK;VIP success:" + key);
                        trans.setEtat("OK;VIP success ");
                        dbtrans.insertTrans(trans);


                        return "OK;VIP success " + modele;
                    } else {

                        System.out.println("NOTOK;utiliser VIP 2 fois :" + key);
                        trans.setEtat("NOTOK;utiliser VIP 2 ");
                        dbtrans.insertTrans(trans);

                        return "NOTOK;utiliser VIP 2";
                    }
                } else {
                    System.out.println("NOTOK;VIP port incorrect " + key);
                    trans.setEtat("NOTOK;VIP port incorrect ");
                    dbtrans.insertTrans(trans);
                    return "NOTOK;VIP port incorrect";
                }
            } //-------------------------------Badges Press
            else if (t.equals("Badges Press")) {
                System.out.println("Entrer Presse");
                //return "OK;" + modele;
                if (type.equals("Vip")) {
                    if (b.getBnrAcces() < 1) {

//                        billetDAO.beginTransaction();
                        b.setBnrAcces(b.getBnrAcces() + 1);
                        dbtrans.updateBillet(b);
//                        billetDAO.merge(b);
//                        billetDAO.commit();
                        System.out.println("OK;Press success:" + key);
                        trans.setEtat("OK;Press success ");
                        dbtrans.insertTrans(trans);


                        return "OK;Press success " + modele;
                    } else {
                        System.out.println("NOTOK;utiliser Press " + key);
                        trans.setEtat("NOTOK;utiliser Press ");
                        dbtrans.insertTrans(trans);
                        return "NOTOK;utiliser Press";
                    }
                } else {
                    System.out.println("NOTOK;Port invalide Presse " + key);
                    trans.setEtat("NOTOK;Port invalide Presse ");
                    dbtrans.insertTrans(trans);
                    return "NOTOK;Port invalide Presse";
                }
            } //-------------------------------Badges Press VIP
            else if (t.equals("Badges Press VIP")) {
                System.out.println("Entrer Presse VIP");
                if (type.equals("Vip")) {
                    if (b.getBnrAcces() < 2) {
//                        billetDAO.beginTransaction();
                        b.setBnrAcces(b.getBnrAcces() + 1);
                        dbtrans.updateBillet(b);
//                        billetDAO.merge(b);
//                        billetDAO.commit();
                        System.out.println("OK;Press VIP success:" + key);
                        trans.setEtat("OK;Press VIP success ");
                        dbtrans.insertTrans(trans);

                        return "OK;OK;Press VIP success " + modele;
                    } else {

                        System.out.println("NOTOK;Press VIP Utiliser 2:" + key);
                        trans.setEtat("NOTOK;Press VIP Utiliser 2 ");
                        dbtrans.insertTrans(trans);

                        return "NOTOK;Press VIP Utiliser 2";
                    }
                } else {
                    System.out.println("NOTOK;Press VIP Port incorrecte:" + key);
                    trans.setEtat("NOTOK;Press VIP Port incorrecte ");
                    dbtrans.insertTrans(trans);

                    return "NOTOK;Press VIP Port incorrecte";
                }
            } //-------------------------------KIT "00400205389300-KT88" "10400205808300-KT88"
            else if (t.equals("KIT") || t.equals("KIT A") || t.equals("KIT B")) {
                if (t.equals("KIT A") || t.equals("KIT B")) {
                    if (b.getUtilise() == false) {
//                        billetDAO.beginTransaction();
                        b.setUtilise(true);
                        dbtrans.updateBillet(b);
//                        billetDAO.merge(b);
//                        billetDAO.commit();
                        System.out.println("OK;KIT A success:" + key);
                        trans.setEtat("OK;KIT A success ");
                        dbtrans.insertTrans(trans);
                        return "OK;KIT A success: ";

                    } else {
                        System.out.println("NOTOK;KIT A utiliser:" + key);
                        trans.setEtat("NOTOK;KIT A utiliser: ");
                        dbtrans.insertTrans(trans);

                        return "NOTOK;KIT A utiliser";
                    }
                } else {
                    System.out.println("Entrer KIT");
                    List<Kit> k = new KitDAO().findByBillet(b.getId());
                    System.out.println("size=" + k.size());
                    String s = k.get(0).getIdsSpec();
                    System.out.println("s kit=" + s);
                    if (type.equals("Public")) {
                        System.out.println(" s.substring(0, s.indexOf(\";\"))=" + s.substring(0, s.indexOf(";")));
                        if (s.contains(";" + evenement.getId() + ";") || s.substring(0, s.indexOf(";")).equals(evenement.getId() + "")) {
                            if (b.getUtilise() == false) {
//                            billetDAO.beginTransaction();
                                b.setUtilise(true);
                                dbtrans.updateBillet(b);
//                            billetDAO.merge(b);
//                            billetDAO.commit();
                                System.out.println("OK;KIT success:" + key);
                                trans.setEtat("OK;KIT success ");
                                dbtrans.insertTrans(trans);
                                return "OK;KIT success: ";

                            } else {
                                System.out.println("NOTOK;KIT utiliser:" + key);
                                trans.setEtat("NOTOK;KIT utiliser: ");
                                dbtrans.insertTrans(trans);

                                return "NOTOK;KIT utiliser";
                            }
                        } else {
                            System.out.println("NOTOK;KIT Spectacle non inclu:" + key);
                            trans.setEtat("NOTOK;KIT Spectacle non inclu: ");
                            dbtrans.insertTrans(trans);

                            return "NOTOK;KIT Spectacle non inclu";
                        }

                    } else {
                        System.out.println("NOTOK;Porte Incorrecte KIT" + key);
                        trans.setEtat("NOTOK;Porte Incorrecte KIT ");
                        dbtrans.insertTrans(trans);
                        return "NOTOK;Porte Incorrecte KIT";
                    }
                }
            } //-------------------------------Pass badge 49930189235200
            else if (t.equals("Pass") || t.equals("Badges comite direction")) {
                System.out.println("Pass ou Badges comites");
                System.out.println("OK;Success Badges comite direction" + key);
                trans.setEtat("OK;Success Badges comite direction ");
                dbtrans.insertTrans(trans);
                return "OK;Success Badges comite direction";
            } //-----------------------------Staff
            else if (t.equals("Badges staff organisation")) {
                System.out.println("Entrer Staff organisation");

                if (b.getBnrAcces() < 1) {
//                    billetDAO.beginTransaction();
                    b.setBnrAcces(b.getBnrAcces() + 1);
                    dbtrans.updateBillet(b);
//                    billetDAO.merge(b);
//                    billetDAO.commit();
                    System.out.println("OK;Success Entrer Staff organisation" + key);
                    trans.setEtat("OK;Success Entrer Staff organisation ");
                    dbtrans.insertTrans(trans);
                    return "OK;Entrer success Staff organisation";
                } else {
                    System.out.println("NOTOK;utiliser Staff organisation utiliser" + key);
                    trans.setEtat("NOTOK;utiliser Staff organisation utiliser ");
                    dbtrans.insertTrans(trans);
                    return "NOTOK;NOTOK;utiliser Staff organisation ";
                }

            } //-------------------------------Badges Artiste
            else if (t.equals("Badges artistes")) {
                System.out.println("Entrer Artiste");

                if (b.getBnrAcces() < 2) {
//                    billetDAO.beginTransaction();
                    b.setBnrAcces(b.getBnrAcces() + 1);
                    dbtrans.updateBillet(b);
//                    billetDAO.merge(b);
//                    billetDAO.commit();
                    System.out.println("OK;Badges artistes" + key);
                    trans.setEtat("OK;Badges artistes ");
                    dbtrans.insertTrans(trans);
                    return "OK;Badges artistes" + modele;
                } else {
                    System.out.println("NOTOK;NOTOK;utiliser Artistes" + key);
                    trans.setEtat("NOTOK;NOTOK;utiliser Artistes ");
                    dbtrans.insertTrans(trans);

                    return "NOTOK;utiliser Artistes";
                }


            } else {
                System.out.println("*********************************************************");
                b = lists.get(0);
                System.out.println("Numero:" + b.getNumero() + "  ; type :" + b.getTypeb().getTypeBillet().getTypeCarte() + " ;id:" + b.getId()
                        + "vendu :" + b.getVendu() + " utilise :" + b.getUtilise());
                System.out.println("*********************************************************");

                return "NOTOK;je sais pas " + lists.get(0).getNumero();
            }
//            return "NOTOK;je sais pas " + lists.get(0).getNumero();

        }

        // return "NOTOK;je sais pas " + lists.get(0).getNumero();


    }
//    @WebMethod(operationName = "verifyTicket")
//    public String verifyTicket(@WebParam(name = "key") String key, @WebParam(name = "type") String type) {
////        List<Billet> billets=billetDAO.findByProperty("numero", key);
////        if(billets.size()>0)
////            return "OK;"+billets.get(0).getTypeb().getTypeBillet().getModele();
////        else
////            return "NOTOK;utiliser";
//        
//    }
    /**
     * Web service operation
     */
    TransDAO transDAO = new TransDAO();

    @WebMethod(operationName = "getTrans")
    public String getTrans(@WebParam(name = "bcode") String bcode) {
        System.out.println("Request for : " + bcode);
        List<Trans_1> list = transDAO.findByNumero(bcode);
        String s = "";
        for (Trans_1 trans_1 : list) {
            s += trans_1.getId().getEtat() + "$" + trans_1.getId().getTime() + "";
            s+="%";
                    
        }
        return s;
    }
     @WebMethod(operationName = "verifyChaise")
    public String verifyChaise(@WebParam(name = "key") String key,@WebParam(name = "type") String type) throws ParseException {
        System.out.println("------------------------------------------------------------------------");
        key = key.replaceAll("=", "-");
        date = new Date();
        Billet b;
        Onligne o = new Onligne();
        date.setHours(date.getHours() - 7);

        System.out.println("Date=" + date);
        evenement = (Evenement) evenementDAO.findByProperty("date", date).get(0);
        System.out.println("Evenement e" + evenement.getTitre());
        System.out.println("Date=" + date);
        if (!key.contains("OL84")) {
            try {
                b = (Billet) billetDAO.findByProperty("numero", key).get(0);
                int idt = b.getTypeb().getTypeBillet().getId();
                if (idt == 1 || idt == 15) {
                    if (b.getTypeb().getEvenement().equals(evenement)) {
                        if (b.isReserve()) {
                            return "Utiliser";

                        } else {
                            billetDAO.beginTransaction();
                            b.setReserve(true);
                            billetDAO.merge(b);
                            billetDAO.commit();
                            return "Billet chaise valide";
                        }
                    } else {
                        return "Evenement invalide";
                    }
                } else if (idt == 16 || idt == 21) {
                    return"valide chaise";
                }
                else
                     return"Billet non chaise";

            } catch (IndexOutOfBoundsException e) {
                return "Billet inexistante";
            }

        }

        return null;
    }

    
}
